package com.jcl.dd.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@EnableConfigurationProperties(DruidProperties.class)
@Component
public class LoadDataSource {

    @Autowired
    private DruidProperties druidProperties;

    public Map<String, DataSource> loadAllDataSource() {
        Map<String, DataSource> map = new HashMap<>();
        Map<String, Map<String, String>> propertiesDs = druidProperties.getDs();
        Set<String> set = propertiesDs.keySet();
        try {
            for (String s : set) {
                map.put(s, druidProperties.dataSource((DruidDataSource) DruidDataSourceFactory.createDataSource(propertiesDs.get(s))));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

}
